/**
 *
 *@author ZhouLan
 *@version 1.1.0
 */

import axios,{AxiosResponse} from '@ohos/axios'
import { ApplicationConfig } from '../entity/ApplicationConfig'
import HashMap from '@ohos.util.HashMap'
import {getRouter,postRouter} from '../configs/routerConfig'
import {QueryParam} from '../entity/UrlRouter'
import { Order } from '../entity/Communications'
import {GoodsDetail} from '../entity/GoodsDetailContent'

let applicationConfig =  globalThis.applicationConfig as ApplicationConfig
export class Request{
  rootUrl:string
  getRoutersMap:HashMap<string,string>
  postRoutersMap:HashMap<string,any>

  constructor(rootUrl:string,routerMap?:HashMap<string,string>) {
    this.rootUrl = rootUrl
    this.getRoutersMap = new HashMap<string,string>()
    this.postRoutersMap = new HashMap<string,any>()
    this.init()
  }
  // async  getUserIdByAdmin(admin:string,password:string){
  //   //通过账户名获取
  //   // return this.getAsync("/getUserId")
  // }
  async getNickNameByAdmin(admin:string){
    return await this.getAsync("getNickname",'account',admin)
  }
  async getAsync(mapKey:string,query:string,value:string|number){
    console.log('http:'+mapKey)
    console.log('http'+this.getRoutersMap.get(mapKey))
    console.log(`get:${this.rootUrl+this.getRoutersMap.get(mapKey)}?${query}=${value}`)
    let result = await axios.get<any,AxiosResponse<any>>(`${this.rootUrl+this.getRoutersMap.get(mapKey)}?${query}=${value}`)
    return result.data
  }
  //如果有多条query,则使用这个函数
  async getByQueries(mapKey:string,queries:QueryParam[]){
    let query = ''
    for (let i = 0; i < queries.length; i++) {
      query+=`${queries[i].key}=${queries[i].value}`
      if(i<queries.length-1){
        query+='&'
      }
    }
    let url
    //如果长度为0说明没有参数
    if(queries.length){
      url = `${this.rootUrl}${this.getRoutersMap.get(mapKey)}?${query}`
    }else{
      url = `${this.rootUrl}${this.getRoutersMap.get(mapKey)}`
    }
    console.log("url:"+url)
    let result = await axios.get<any,AxiosResponse<any>>(url)
    return result.data
  }
  async postAsync(mapKey:string,params:{}){
    let url = this.rootUrl+this.postRoutersMap.get(mapKey)
    console.log(`post:${url}`)
    let result = await axios.post<any,AxiosResponse<any>>(url,params)
    return result.data
  }
  getIconUrlByAccount(account:string){
    return this.rootUrl+"/getIcon?account="+account
  }
  setAGetUrl(key:string,value:string){
    this.getRoutersMap.set(key,value)
  }
  setAPostUrl(key:string,value:any){
    this.postRoutersMap.set(key,value)
  }
  private init(){
    for (let i = 0; i < getRouter.length; i++) {
      this.getRoutersMap.set(getRouter[i].key,getRouter[i].value)
    }
    for(let i = 0; i < postRouter.length;i++){
      this.postRoutersMap.set(postRouter[i].key,postRouter[i].value)
    }
  }
  async getSharePicsName(shareId:string|number):Promise<string[]>{
    let result = await this.getAsync('getShareImgNames','shareId',shareId)
    return result
  }
  async getGoodsPicsName(goodsId:string|number):Promise<string[]>{
    let result = await this.getAsync('getGoodsImgNames','goodsId',goodsId)
    return result
  }
  async getGoodsId(businessId:string|number,stamp:string|number){
    let account:QueryParam = {
      key:'businessId',
      value:businessId
    }
    let timestamp:QueryParam = {
      key:'timestamp',
      value:stamp
    }
    return this.getByQueries('getGoodsId',[account,timestamp])
  }
  async getShareId(admin:string|number,stamp:string|number){
    let account:QueryParam = {
      key:'account',
      value:admin
    }
    let timestamp:QueryParam = {
      key:'timestamp',
      value:stamp
    }
    return this.getByQueries('getShareId',[account,timestamp])
  }
  // async getAllShares():Promise<ShareThing[]>{
  //   return await this.getByQueries('getShares',[])
  // }

  getSharePicsUploadUrl(){
    return this.rootUrl+this.postRoutersMap.get('uploadSharePics')
  }
  getGoodsPicsUploadUrl(){
    return this.rootUrl+this.postRoutersMap.get('uploadGoodsPics')
  }
  async getSharePicUrls(id:number|string):Promise<string[]>{
    let picsName = await this.getSharePicsName(id)
    let picUrls:string[] = []
    for (let i = 0; i < picsName.length; i++) {
      picUrls.push(`${this.rootUrl}${this.getRoutersMap.get("getSharePic")}?shareId=${id}&fileName=${picsName[i]}`)
    }
    console.log(`picUrls:${picUrls.toString()}`)
    return picUrls
  }
  async getGoodsPicUrls(id:number|string):Promise<string[]>{
    let picsName = await this.getGoodsPicsName(id)
    let picUrls:string[] = []
    for (let i = 0; i < picsName.length; i++) {
      picUrls.push(`${this.rootUrl}${this.getRoutersMap.get("getGoodsPic")}?goodsId=${id}&fileName=${picsName[i]}`)
    }
    return picUrls
  }
  async getFirstGoodsPicUrl(goodsId:string|number){
    let picsName = await this.getGoodsPicsName(goodsId)
    console.log(`firstPic:${this.rootUrl}${this.getRoutersMap.get("getGoodsPic")}?goodsId=${goodsId}&fileName=${picsName[0]}`)
    return `${this.rootUrl}${this.getRoutersMap.get("getGoodsPic")}?goodsId=${goodsId}&fileName=${picsName[0]}`
  }
  async getBusinessIdByGoodsId(goodsId:string|number){
    return await this.getAsync("getBusinessId","goodsId",goodsId)
  }
  async getShopNameByAccount(account:string){
    let shopName = await this.getAsync("getShopName",'businessId',account)
    console.log(`shopName:${shopName}}`)
    return shopName
  }
  async getOrdersByBusinessId(businessId:string):Promise<Order[]>{
    return await this.getAsync("getOrdersByBusinessId",'businessId',businessId)
  }
  //当前的账户名获取所有的订单
  async getAllOrders(account:string):Promise<Order[]>{
    return await this.getAsync("getOrdersByBusinessId",'account',account)
  }
  async setShopNameByAccount(account:string,sname:string):Promise<string>{
    let businessId:QueryParam = {
      key:'account',
      value:account
    }
    let shopName:QueryParam = {
      key:'shopName',
      value:sname
    }
    return await this.getByQueries("setShopName",[businessId,shopName])
  }
  async getGoodsDetailByAccount(account:string):Promise<GoodsDetail[]>{
    return await this.getAsync('getDetails','businessId',account)
  }
  async addGoods(goodsDetail:GoodsDetail){
    return await this.postAsync('addGoods',goodsDetail)
  }
  async updateGoods(goodsDetail:GoodsDetail){
    return await this.postAsync('updateGoods',goodsDetail)
  }
  async updateOrderState(order:Order):Promise<boolean>{
    return await this.postAsync('updateOrderState',order)
  }
}